package com.sihai.ehr.service.impl;

import com.sihai.core.*;
import com.sihai.ehr.model.biz.AdministrationOutApplyForm;
import com.sihai.ehr.model.biz.AdministrationOutApplyFormAuditing;
import com.sihai.ehr.model.sys.Organization;
import com.sihai.ehr.model.sys.User;
import com.sihai.ehr.repository.AuditingRepository;
import com.sihai.ehr.service.AdministrationOutApplyFormService;
import com.sihai.ehr.web.pojo.SearchFormPojo;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.List;

/**
 * author: wxn
 * since: 13-4-15 22:10
 * 行政签核系统 - 出差申请单Service
 */
@Service("administrationOutApplyFormService")
public class AdministrationOutApplyFormServiceImpl implements AdministrationOutApplyFormService {
    @Resource
    private DataAccessor dataAccessor;
    @Resource
    private AuditingRepository auditingRepository;

    @Override
    public void save(AdministrationOutApplyForm form) throws Exception {
        Assert.notNull(form, "AdministrationOutApplyForm is required");
        boolean isCreate = (form.getId() == null || form.getId() <= 0);
        dataAccessor.save(form);
        if(isCreate){
            saveAdministrationOutApplyFormAuditing(form, 1);
        }
    }

    @Override
    public AdministrationOutApplyForm get(Long id) throws Exception {
        Assert.notNull(id, "AdministrationOutApplyForm id is required");
        return dataAccessor.get(AdministrationOutApplyForm.class, id);
    }

    @Override
    public boolean delete(String ids) throws Exception {
        final String deleteSql = String.format("delete from AdministrationOutApplyForm frt where frt.id in (%s)", ids);
        this.dataAccessor.batchUpdate(new QueryCreator() {
            @Override
            public Query createQuery(Session session) {
                return session.createQuery(deleteSql);
            }
        });
        return true;
    }

    @Override
    public LimitedList<AdministrationOutApplyForm> findAdministrationOutApplyForm(User user, final SearchFormPojo searchFormPojo, Limit limit, Sorter sorter) throws Exception {
        if(user == null || user.getWork() == null){
            if(searchFormPojo == null){
                return dataAccessor.findByHql(AdministrationOutApplyForm.class, "from AdministrationOutApplyForm", null, limit, sorter);
            }
            return dataAccessor.findByQuery(AdministrationOutApplyForm.class, new QueryCreator(){
                @Override
                public Query createQuery(Session session) {
                    DynamicQuery query = new DynamicQuery("from AdministrationOutApplyForm f where 1=1");
                    query.appendIfNotNull("and f.create_date > :startDate","startDate", searchFormPojo.getStartDate());
                    query.appendIfNotNull("and f.create_date < :endDate","endDate", searchFormPojo.getEndDate());
                    //query.appendIfNotNull("and f.work.organization.id = :organizationId", "organizationId", searchFormPojo.getOrgId());
                    if(searchFormPojo.getOrgId() != null && searchFormPojo.getOrgId() > 0){
                        Organization o = dataAccessor.get(Organization.class, searchFormPojo.getOrgId());
                        if(o != null){
                            query.append("and f.work.organization.treePath like '" + o.getTreePath() + "%'");
                        }
                    }
                    query.appendIfNotNull("and f.work.duty.id = :dutyId", "dutyId", searchFormPojo.getDutyId());
                    return query.build(session);
                }
            }, limit, sorter);
        }
        return dataAccessor.findByHql(AdministrationOutApplyForm.class, "from AdministrationOutApplyForm f where f.work.id=?", new Object[]{user.getWork().getId()}, limit, sorter);
    }

    //查询用款申请单审核流程记录
    @Override
    public List<List<AdministrationOutApplyFormAuditing>> findAdministrationOutApplyFormAuditing(Long formId) throws Exception {
        return auditingRepository.findByHql(AdministrationOutApplyFormAuditing.class, "from AdministrationOutApplyFormAuditing ta where ta.form.id = ?", new Object[]{formId});
    }

    //保存用款申请单审核流程记录
    @Override
    public void saveAdministrationOutApplyFormAuditingProcess(Long formId) {
        saveAdministrationOutApplyFormAuditing(dataAccessor.get(AdministrationOutApplyForm.class, formId), 1);
    }

    //创建审核流程
    private void saveAdministrationOutApplyFormAuditing(AdministrationOutApplyForm form, int auditNo) {
        auditingRepository.saveAuditingProcess(form, auditNo);
    }
}
